[jQuery 1.4] move one div over another

Posted by Tomasz Zielinski on Stack Overflow See other posts from Stack Overflow or by Tomasz Zielinski
Published on 2010-04-08T11:52:20Z Indexed on 2010/04/08 12:13 UTC
Read the original article Hit count: 271

Filed under:
|
|

I have two div elements that are twins (i.e. their dimensions and contents are identical). I want to move of those div-s over another, so that their corners are at exactly the same coordinates. What I try to do is:

var offset = $('div#placeholder').offset();
$('div#overlay').css('position', 'absolute').css('left', offset.left + 'px').css('top', offset.top + 'px')

-- but this causes the overlay to be exactly (or almost exactly, taking subpixel accuracy into account) 16px below the placeholder (below, i.e. overlay_top = placeholder_top + 16px).

I'm aware that offset() gives position relative to the document, and position: absolute sets position relative to body element, but compensating for body offset() doesn't help much (I'm getting 8px offset, equal to margins):

offset.top -= $('body').offset().top;
offset.left -= $('body').offset().left;

Also, compensating for body margins (in case they were different that offset() didn't help, as they were set to 8px).

Does somebody know what I'm doing wrong here?

UPDATE: Take a look at here - I'm getting the same result in FireFox 3.6.3 and Opera 10.10.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about dom